package com.itheima.tanhua.controller;

import com.itheima.tanhua.service.SmallVideosService;
import com.itheima.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author Lff
 * @date 2022/4/3
 * <p>
 * 视频上传下载
 */
@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {
    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 上传视频
     *
     * @param videoThumbnail 视频封面
     * @param videoFile      视频文件
     */
    @PostMapping
    public ResponseEntity uploadVideos(MultipartFile videoThumbnail, MultipartFile videoFile) {
        smallVideosService.uploadVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看视频列表。
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getVideosList(@RequestParam("page") Integer page,//当前页码
                                        @RequestParam("pagesize") Integer pagesize) {//页尺寸

        PageResult pr = smallVideosService.findVideosList(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * /smallVideos/:id/comments
     * 视频评论发布
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity publishVideoComments(@PathVariable String id, @RequestBody Map map) {
        String comment = map.get("comment").toString();
        Integer count = smallVideosService.publishVideoComments(id, comment);
        return ResponseEntity.ok(count);
    }

    /**
     * /smallVideos/:id/comments
     * 评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity publishVideoComments(@PathVariable String id,
                                               @RequestParam("page") Integer page,//当前页码
                                               @RequestParam("pagesize") Integer pagesize) {
        PageResult pr = smallVideosService.findAllVideoComments(id, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 视频点赞
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity videoLike(@PathVariable String id) {
        Integer count = smallVideosService.videoLike(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频取消点赞
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity videoDisLike(@PathVariable String id) {
        Integer count = smallVideosService.videoDisLike(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频评论点赞
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String videoId) {
        Integer count = smallVideosService.likeComment(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频评论取消点赞
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity disLikeComment(@PathVariable("id") String videoId) {
        Integer count = smallVideosService.disLikeComment(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 关注视频作者
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long userId) {
        smallVideosService.toUserFocus(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long userId) {
        smallVideosService.toUserUnFocus(userId);
        return ResponseEntity.ok(null);
    }
}
